<template>
  <el-dialog :title="title" :visible.sync="visible" class="dialog-sm" append-to-body @close="close">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-form-item label="岗位名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入岗位名称" maxlength="64"/>
      </el-form-item>
      <el-form-item label="岗位编码" prop="pos_key">
        <el-input v-model="form.pos_key" placeholder="请输入编码名称" maxlength="64"/>
      </el-form-item>
      <el-form-item label="岗位顺序" prop="list_sort">
        <el-input-number v-model="form.list_sort" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="岗位状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.value"
          >{{dict.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="250"/>
      </el-form-item>
    </el-form>
    <div class="dialog-footer slot">
      <el-button type="primary" @click="handleSubmit" :loading="submitting" :disabled="loading">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { infoPostApi, editPostApi, addPostApi } from '@/api/system/post'

  export default {
    name: 'PostForm',
    dicts: ['sys_normal_disable'],
    props: {
      value: Boolean,
      id: Number|String,
      moduleName: String
    },
    data() {
      return {
        title: '',
        loading: false,
        submitting: false,
        form: {
          name: undefined,
          pos_key: undefined,
          status: '1',
          list_sort: '10',
          remark: undefined
        },
        rules: {
          name: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
          pos_key: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
          list_sort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }]
        }
      }
    },
    computed: {
      visible: {
        get() {
          return this.value
        },
        set(val) {
          this.$emit('input', val)
        }
      }
    },
    created() {
      this.init()
    },
    methods: {
      async init() {
        this.loading = true
        this.form.id = this.id || undefined
        if (this.form.id) {
          this.title = '编辑' + this.moduleName
          await this.getRowData()
        } else {
          this.title = '添加' + this.moduleName
        }
        this.loading = false
      },
      async handleSubmit() {
        this.$refs.formRef.validate(async valid=>{
          if(!valid || this.submitting) return
          this.submitting = true
          try {
            await this.$modal.confirm('确定提交表单数据？')
            if (this.form.id) {
              await editPostApi(this.form)
            } else {
              await addPostApi(this.form)
            }
            this.close(true, '操作成功')
          } catch (e) {
            this.submitting = false
          }
        })
      },
      async getRowData() {
        try {
          const { info } = await infoPostApi(this.form.id)
          this.form = this.dataToForm(this.form, info)
        } catch (error) {
          this.close()
        }
      },
      close(submit = false, msg) {
        if (submit === true) this.$emit('submit')
        this.$emit('close', submit === true)
        if (msg) submit === true ? this.$modal.msgSuccess(msg) : this.$modal.msgError(msg)
        this.visible = false
      },
    }
  }
</script>
